var hzkx = {
    setBtnStatus: function () {
        var row = $('#grid_hzkx').datagrid('getSelected');
        if (row && row.id && row.zt) {
            if (row.zt == 'ytj') {//已保存
                $("#btn_hzkx_sh").show(); // 划转确认
                $("#btn_hzkx_hz").hide(); // 划转划转
            } else if (row.zt == 'ytz') {
                $("#btn_hzkx_sh").hide(); // 划转确认
                $("#btn_hzkx_hz").show(); // 划转划转
            } else {
                $("#btn_hzkx_sh").hide(); // 划转确认
                $("#btn_hzkx_hz").hide(); // 划转划转
            }
        } else {
            $("#btn_hzkx_sh").hide(); // 划转确认
            $("#btn_hzkx_hz").hide(); // 划转划转
        }
    },
    alwayShowDetail: function (rowData) {
        $('#layout_hzkx').layout('remove', 'center');
        $('#layout_hzkx').layout('add', {
            region: 'center',
            split: true,
            href: $.webPath() + '/sfgl/hzkx/hzkx_detail?hzkxId=' + rowData.id
        });
    }
}
var clickRowIndex = null;
$(function () {
    //加载企业机构
    $("#hzkx_gldw").gsjgqczcombobox();
    var sysDate = $("#hzkx_sysDate").val();
    //表格初始加载
    $('#grid_hzkx').datagrid({
        url: $.webPath() + "/sfgl/hzkx/hzkxList",
        toolbar: "#toolbar_hzkx",
        fitColumns: true,
        singleSelect: true,
        pagination: true,
        pageNumber: 1,
        fit: true,
        rownumbers: true,
        striped: true,
        columns: [
            [
                {field: 'bm', title: '单据号', width: 25, align: 'center'},
                /* {field: 'pjhm', title: '票据号码', width: 25, align: 'center'},*/
                {field: 'nbhzsfk_bm', title: '原单据号', width: 25, align: 'center'},
                {field: 'fplx', title: '款项类型', width: 15,
                    formatter: function (value) {
                        if (value == '0')
                            return '非计划现货';
                        if (value == '1')
                            return '物流金融';

                    }, align: 'center'
                },
                /*{field: 'sfklx', title: '收款类型', width: 15,
                 formatter: function (value) {
                 if (value == '3')
                 return '销售收款';
                 else
                 return '供应退款';
                 }, align: 'center'
                 },*/
                {field: 'gldw_qc', title: '关联企业', width: 40, align: 'center'},
                {field: 'sfje', title: '收款金额(元)', width: 25,
                    formatter: function (value) {
                        var sl = formatJe2(value, ",");
                        return sl;
                    },
                    styler: function () {
                        return 'color:green;font-size:14px;';
                    }, align: 'right'
                },
                /* {field: 'kxlx', title: '款项类型', width: 15,
                 formatter: function (value) {
                 if (value == '1')
                 return '预付款';
                 if (value == '2')
                 return '应付款';
                 if (value == '3')
                 return '预收款';
                 if (value == '4')
                 return '应收款';
                 }, align: 'center'
                 },*/
                {field: 'khfksj', title: '客户付款时间', width: 20,
                    formatter: function (value) {
                        var val = formatDate(sysDate, value, 5);
                        return val
                    }, align: 'center'},
                {field: 'pjlx_z', title: '票据类型', width: 15, align: 'center'},
                {field: 'kxsy_z', title: '款项用途', width: 15, align: 'center'},
                {field: 'fkfs_z', title: '付款方式', width: 15, align: 'center'},
                {field: 'sbgs_jc', title: '申报公司', width: 30, align: 'center'},
                {field: 'sbr_xm', title: '申报人', width: 15, align: 'center'},
                {field: "shrq", title: "审核日期", width: 20, align: 'center'} ,
                {field: "zt", title: "状态", width: 10,
                    formatter: function (value) {
                        if (value == 'ytj') {
                            return '已提交';
                        } else if (value == 'ytz') {
                            return '已调整';
                        }
                    }, align: 'center'},
                {field: 'sfhz', title: '是否划转', width: 15,
                    formatter: function (value) {
                        if (value == '0')
                            return '未划转';
                        if (value == '1')
                            return '已划转';

                    }, align: 'center'
                }
            ]
        ],
        onLoadSuccess: function () {
            var rows = $(this).datagrid("getRows");
            if (rows.length > 0) {
                $(this).datagrid("selectRow", 0);
                clickRowIndex = 0;
            }
            hzkx.setBtnStatus();
            hzkx.alwayShowDetail(rows[0]);
            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        },
        onClickRow: function (rowIndex, rowData) {
            hzkx.setBtnStatus();
            if (clickRowIndex == rowIndex)
                return;
            clickRowIndex = rowIndex;
        },
        onSelect: function (rowIndex, rowData) {
            hzkx.setBtnStatus();
            if (clickRowIndex == rowIndex)
                return;
            clickRowIndex = rowIndex;
            hzkx.alwayShowDetail(rowData);
        }
    });
    //**********按钮触发事件**********
    // 非计划现货到物流金融要进行审核按钮
    $("#btn_hzkx_sh").click(function () {
        hzkx_sh('款项划转后审核');
    });

    // 款项继续划转
    $("#btn_hzkx_hz").click(function () {
        hzkx_hz('款项划转');
    });

    //查询按钮
    $("#btn_hzkx_search").click(function () {
        hzkx_search();
    });

    //加载查询条件中的申报公司下拉框
    $("#hzkx_sbgs").combotree({
        loadFilter: function (rows) {
            return convert(rows);
        }
    });

});

// 划转后审核
function hzkx_sh(title) {
    var row = $('#grid_hzkx').datagrid('getSelected');
    var href = $.webPath() + "/sfgl/hzkx/hzkx_sh_detail?hzkxId=" + row.id;
    var actionUrl = $.webPath() + "/sfgl/hzkx/hzkx_sh";
    hzkx_sh_showDialog(title, href, actionUrl);
}

// 款项划转
function hzkx_hz(title) {
    var row = $('#grid_hzkx').datagrid('getSelected');
    if (row == null) {
        $.messager.alert("提示", "请选择要划转的收款!", "warning");
        return;
    }
    var href = $.webPath() + "/sfgl/hzkx/hzkx_kxhz?hzkxId=" + row.id;
    var actionUrl = $.webPath() + "/sfgl/hzkx/hzkx_kxhz_save";
    hzkx_kxhz_showDialog(title, href, actionUrl);
}

// 划转后审核窗
function hzkx_sh_showDialog(title, href, actionUrl) {
    var win = $.window({
        title: title,
        width: 720,
        height: 460,
        cache: false,
        url: href,
        modal: true,
        buttons: [
            {
                text: '审核',
                handler: function () {
                    $(win).mask();
                    $('#form_hzkx_sh').form('submit', {
                        url: actionUrl,
                        onSubmit: function () {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            }
                            return isValid;
                        },
                        success: function (data) {
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_hzkx').datagrid('reload');
                                $('#layout_sksh').layout('panel', 'center').panel('refresh');
                                $.messager.show({
                                    title: '提示',
                                    msg: json.message,
                                    timeout: 3000,
                                    showType: 'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}

// 弹出应收款划转窗口
function hzkx_kxhz_showDialog(title, href, actionUrl) {
    var win = $.window({
        title: title,
        width: 620,
        height: 360,
        cache: false,
        url: href,
        modal: true,
        buttons: [
            {
                text: '确认',
                handler: function () {
                    $(win).mask();
                    $('#form_hzkx_kxhz').form('submit', {
                        url: actionUrl,
                        onSubmit: function (param) {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            }
                            return isValid;
                        },
                        success: function (data) {
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_hzkx').datagrid('reload');
                                $.messager.show({
                                    title: '提示',
                                    msg: json.message,
                                    timeout: 3000,
                                    showType: 'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}



//查询收款管理
function hzkx_search() {
    var sbgs = $('#hzkx_sbgs').combotree('getText') == null ? null : $('#hzkx_sbgs').combotree('getText');
    var sbgsId = $('#hzkx_sbgs').combotree('getValue') == null ? null : $('#hzkx_sbgs').combotree('getValue');
    var zt = $('#hzkx_zt').combobox('getValue') == null ? null : $('#hzkx_zt').combobox('getValue');
    var hzkxStart = $('#hzkx_start').datebox('getValue');
    var hzkxEnd = $('#hzkx_end').datebox('getValue');
    $('#grid_hzkx').datagrid('load',
        {
            bm: $('#hzkx_bm').val(),
            sbgs: sbgs,
            sbgsId: sbgsId,
            gldwMc: $('#hzkx_gldw').attr("_text") == null ? null : $('#hzkx_gldw').attr("_text"),
            zt: zt,
            skr: $('#hzkx_zdr').val(),
            hzkxStart: hzkxStart,
            hzkxEnd: hzkxEnd,
            ybm: $('#hzkx_ybm').val()
        }
    );
}
